﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceFight
{
	class SimpleModel : IModel
	{
		public Vector3    Position    { get; set; }
		public Vector3    Scale       { get; set; }
		public Quaternion Orientation { get; set; }
		
		Model myModel;
		
		Matrix m_world;

		public SimpleModel(String p_assetName, ContentManager Content)
		{
			myModel = Content.Load<Model>(p_assetName);
		}
		
		public SimpleModel(String p_assetName)
		{
			myModel = Game1.ContentMgr.Load<Model>(p_assetName);
		}
		
		public void Update(GameTime gameTime)
		{
			m_world = Matrix.CreateScale(Scale);
			m_world = Matrix.Transform(m_world, Orientation);
			m_world *= Matrix.CreateTranslation(Position);
		}

		public void Draw(GameTime gameTime)
		{
			// Copy any parent transforms.
			Matrix[] transforms = new Matrix[myModel.Bones.Count];
			myModel.CopyAbsoluteBoneTransformsTo(transforms);
			
			// Draw the model. A model can have multiple meshes, so loop.
			foreach (ModelMesh mesh in myModel.Meshes)
			{
				// This is where the mesh orientation is set, as well as our camera and projection.
				foreach (BasicEffect effect in mesh.Effects)
				{
					effect.EnableDefaultLighting();
					effect.TextureEnabled = true;
					effect.World = transforms[mesh.ParentBone.Index] * m_world;
					effect.View = Camera.getInstance().View;
					effect.Projection = Camera.getInstance().Projection;
				}
				// Draw the mesh, using the effects set above.
				mesh.Draw();
			}
		}
	}
}
